package com.itmumu.springbootlabels.controller;

import com.itmumu.springbootlabels.entity.Labels;
import com.itmumu.springbootlabels.repository.LabelsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @author JachinLin
 * @Description:
 * @Date: Created in 2020-08-19  下午4:07
 * @Modified By:
 */
@RestController
@RequestMapping("/label")
public class LabelsController {
    @Autowired
    private LabelsRepository labelsRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Labels> findAll(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        PageRequest request = PageRequest.of(page,size);
        return labelsRepository.findAll(request);
    }

    @PostMapping("/save")
    public String save(@RequestBody Labels labels){
        Labels result = labelsRepository.save(labels);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }
    @GetMapping("/findById/{id}")
    public Labels findById(@PathVariable("id")Integer id){
        return labelsRepository.findById(id).get();
    }

    @PutMapping("/update")
    public String update(@RequestBody Labels labels){
        Labels result = labelsRepository.save(labels);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        labelsRepository.deleteById(id);
    }

}
